package com.lxl.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lxl.config.SystemConfig;
import com.lxl.message.JsonMsgProvider;
import com.lxl.message.MsgStatus;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

public class DeleteServlet extends HttpServlet {
    private static Logger logger = Logger.getLogger(DeleteServlet.class);

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if (request.getParameter("type") == null || request.getParameter("path") == null) {
            PrintWriter writer = response.getWriter();
            writer.println(new JsonMsgProvider().provideMessage(MsgStatus.REQUEST_ERROR, "Request Error."));
            writer.flush();
            return;
        }

        String type = request.getParameter("type");
        String path =  SystemConfig.SERVER_ROOT_PATH + File.separator + request.getParameter("path").replace('_',File.separatorChar);

        if ("trans".equals(type)){
            path += SystemConfig.SUFFIX_TRANS;
        } else if ("job".equals(type)){
            path += SystemConfig.SUFFIX_JOB;
        }

        File file = new File(path);
        if (file.exists() && file.isFile()){
            file.delete();
            logger.info("Delete file: " + path);
            response.getWriter().write(new JsonMsgProvider().provideMessage(MsgStatus.SUCCESS,null));
        } else if (!file.exists()){
            response.getWriter().write(new JsonMsgProvider().provideMessage(MsgStatus.FILE_ERROR, "File not exist."));
        } else if (!file.isFile()){
            response.getWriter().write(new JsonMsgProvider().provideMessage(MsgStatus.FILE_ERROR, "It's not file."));
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestJsonStr = IOUtils.toString(request.getInputStream());
        JSONObject requestJsonObj = JSON.parseObject(requestJsonStr);
        String deleteDirPath = SystemConfig.SERVER_ROOT_PATH + File.separator + requestJsonObj.getString("dir").replace('_',File.separatorChar) + File.separator;
        JSONArray array = requestJsonObj.getJSONArray("files");
        int arraySize = array.size();
        for (int i=0;i<arraySize;i++){
            File tempFile = new File(deleteDirPath+array.getString(i));
            if (tempFile.exists() && tempFile.isFile()){
                tempFile.delete();
                logger.info("Delete file: " + deleteDirPath+array.getString(i));
            }
        }

        response.getWriter().write(new JsonMsgProvider().provideMessage(MsgStatus.SUCCESS,null));
    }
}
